//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.views.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.permission.model.organize.OrganizeDepartSelectorListVO;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.RedisUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import ciicos.views.entity.ViewsHmiEntity;
import ciicos.views.model.viewshmi.OrganizeAndViewsVO;
import ciicos.views.model.viewshmi.ViewsHmiForm;
import ciicos.views.model.viewshmi.ViewsHmiInfoVO;
import ciicos.views.model.viewshmi.ViewsHmiListVO;
import ciicos.views.model.viewshmi.ViewsHmiPagination;
import ciicos.views.service.ViewsHmiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"视图管理"},
    value = "viewsManage"
)
@RequestMapping({"/api/viewsManage/ViewsHmi"})
public class ViewsHmiController {
    private static final Logger log = LoggerFactory.getLogger(ViewsHmiController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private ViewsHmiService viewsHmiService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody ViewsHmiPagination viewsHmiPagination) throws IOException {
        List<ViewsHmiEntity> list = this.viewsHmiService.getList(viewsHmiPagination);
        List<ViewsHmiListVO> listVO = JsonUtil.getJsonToList(list, ViewsHmiListVO.class);

        for(ViewsHmiListVO viewsHmiVO : listVO) {
            viewsHmiVO.setOrganizeid_name(this.generaterSwapUtil.comSelectValues(viewsHmiVO.getOrganizeid(), false));
            viewsHmiVO.setName_name(viewsHmiVO.getName());
            viewsHmiVO.setWidth_name(viewsHmiVO.getWidth());
            viewsHmiVO.setHeight_name(viewsHmiVO.getHeight());
            viewsHmiVO.setMargin_name(viewsHmiVO.getMargin());
            viewsHmiVO.setBkcolor_name(viewsHmiVO.getBkcolor());
            viewsHmiVO.setBkimg_name(this.generaterSwapUtil.getFileNameInJson(viewsHmiVO.getBkimg()));
            viewsHmiVO.setCreatoruserid_name(this.generaterSwapUtil.userSelectValue(viewsHmiVO.getCreatoruserid()));
            viewsHmiVO.setCreatortime_name(viewsHmiVO.getCreatortime());
            viewsHmiVO.setLastmodifyuserid_name(this.generaterSwapUtil.userSelectValue(viewsHmiVO.getLastmodifyuserid()));
            viewsHmiVO.setLastmodifytime_name(viewsHmiVO.getLastmodifytime());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(viewsHmiPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ViewsHmiForm viewsHmiForm) throws DataException {
        String b = this.viewsHmiService.checkForm(viewsHmiForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            viewsHmiForm.setCreatoruserid(userInfo.getUserId());
            viewsHmiForm.setCreatortime(DateUtil.getNow());
            ViewsHmiEntity entity = (ViewsHmiEntity)JsonUtil.getJsonToBean(viewsHmiForm, ViewsHmiEntity.class);
            entity.setId(mainId);
            this.viewsHmiService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<ViewsHmiInfoVO> info(@PathVariable("id") String id) {
        ViewsHmiEntity entity = this.viewsHmiService.getInfo(id);
        ViewsHmiInfoVO vo = (ViewsHmiInfoVO)JsonUtil.getJsonToBean(entity, ViewsHmiInfoVO.class);
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        if (vo.getCreatortime() != null) {
            vo.setCreatortime(vo.getCreatortime());
        }

        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        if (vo.getLastmodifytime() != null) {
            vo.setLastmodifytime(vo.getLastmodifytime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<ViewsHmiInfoVO> detailInfo(@PathVariable("id") String id) {
        ViewsHmiEntity entity = this.viewsHmiService.getInfo(id);
        ViewsHmiInfoVO vo = (ViewsHmiInfoVO)JsonUtil.getJsonToBean(entity, ViewsHmiInfoVO.class);
        Map<String, Object> profile = new HashMap();
        profile.put("width", vo.getWidth());
        profile.put("height", vo.getHeight());
        profile.put("margin", vo.getMargin());
        profile.put("bkcolor", vo.getBkcolor());
        profile.put("bkimg", vo.getBkimg());
        vo.setProfile(profile);
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ViewsHmiForm viewsHmiForm) throws DataException {
        String b = this.viewsHmiService.checkForm(viewsHmiForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            UserInfo userInfo = this.userProvider.get();
            ViewsHmiEntity entity = this.viewsHmiService.getInfo(id);
            if (entity != null) {
                viewsHmiForm.setLastmodifyuserid(userInfo.getUserId());
                viewsHmiForm.setLastmodifytime(DateUtil.getNow());
                ViewsHmiEntity subentity = (ViewsHmiEntity)JsonUtil.getJsonToBean(viewsHmiForm, ViewsHmiEntity.class);
                subentity.setCreatoruserid(entity.getCreatoruserid());
                subentity.setCreatortime(entity.getCreatortime());
                boolean b1 = this.viewsHmiService.updateById(subentity);
                if (!b1) {
                    return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
                } else {
                    String names = (String)this.redisUtil.getString("viewNames:" + id);
                    if (StringUtil.isNotEmpty(names)) {
                        this.redisUtil.remove("viewNames:" + id);
                    }

                    return ActionResult.success("更新成功");
                }
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ViewsHmiEntity entity = this.viewsHmiService.getInfo(id);
        if (entity != null) {
            this.viewsHmiService.delete(entity);
        }

        return ActionResult.success("删除成功");
    }

    @ApiOperation("复制")
    @GetMapping({"/copy/{id}"})
    @Transactional
    public ActionResult copy(@PathVariable("id") String id) {
        ViewsHmiEntity entity = this.viewsHmiService.getInfo(id);
        if (entity != null) {
            UserInfo userInfo = this.userProvider.get();
            entity.setCreatoruserid(userInfo.getUserId());
            entity.setCreatortime(DateUtil.getNowDate());
            entity.setLastmodifyuserid((String)null);
            entity.setLastmodifytime((Date)null);
            entity.setName(entity.getName() + "（拷贝）");
            entity.setId(RandomUtil.uuId());
            this.viewsHmiService.create(entity);
        }

        return ActionResult.success("复制成功");
    }

    @GetMapping({"/Selector"})
    public ActionResult listSelect() {
        List<OrganizeDepartSelectorListVO> departments = this.viewsHmiService.getDepartment();
        List<OrganizeAndViewsVO> organizeAndViewsVOS = JsonUtil.getJsonToList(departments, OrganizeAndViewsVO.class);
        this.recursiveOrganizeAndViews(organizeAndViewsVOS);
        return ActionResult.success(organizeAndViewsVOS);
    }

    public void recursiveOrganizeAndViews(List<OrganizeAndViewsVO> organizeAndViewsVOS) {
        for(OrganizeAndViewsVO department : organizeAndViewsVOS) {
            List<OrganizeAndViewsVO> viewsHmiListVO = this.viewsHmiService.getListByDepartment(department.getId());
            if (viewsHmiListVO.size() != 0) {
                department.setChildren(viewsHmiListVO);
                department.setHasChildren(true);
            }

            if (department.getChildren() != null && department.getChildren().size() > 0) {
                this.recursiveOrganizeAndViews(department.getChildren());
            }
        }

    }
}
